<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%
  String voteId=request.getParameter("voteId");
  String voteId1=voteId.replaceAll("-","");
  String isclosed=request.getParameter("isclosed");
%>
<script>
var color=new Array('#71BF44','#532D80','#CD4C2C','#1F9DD8','#71BF44','#532D80','#CD4C2C','#1F9DD8');
function getJsonVote_<%=voteId1 %>(voteId){
	 $.ajax({
			url: '/sapfree/servlet/MessageBoradServlet?method=ajaxGetVoteById&voteId='+voteId, 
			dataType: 'text',
			type: "post",
			error:function(XMLHttpRequest,textStatus,errorThrown){alert(errorThrown);},
			success: function(data) {
			
			     initVoteTable_<%=voteId1 %>(data);
			     $("#voteWait_<%=voteId1 %>").hide();
			}
		});
}

function initVoteTable_<%=voteId1 %>(data){
   $('.voteTr_<%=voteId1 %>').remove();
	data=eval(data);
	var s="";
	var count=0;
	  for(i=0;i<data.length;i++){
	   if(data[i].vo_votecount!= undefined){
         count+=parseInt(data[i].vo_votecount);
         }
     }   
     for(i=0;i<data.length;i++){
         s+=initVoteTR_<%=voteId1 %>(data[i],count,color[i]);
     } 
      $("#option_<%=voteId1 %>").after(s);
}

function initVoteTR_<%=voteId1 %>(message,count,cuColor){
 
    var s="";
    var num=message.vo_votecount;
    var baifen=0;
   
    if(count==0){
     baifen=0;
    }else{
      baifen=num/count;
    }
   
    var result = Math.round(baifen*10000)/100;
    var vc=message.vo_votecount;
    if(message.vo_votecount==undefined){
       vc=0;
    }
     
    var r2=100-result;  
    s+="\n<tr class='voteTr_<%=voteId1 %>'> <td width=\"41%\" height=\"25\" class=\"blackNormal14\">";
    s+="<input type=\"checkbox\" name=\"voteInput_<%=voteId%>\" value="+message.vo_uuid+">&nbsp;";
    s+=message.vo_votetitle+"</td> ";
    s+="  <td width=\"49%\" class=\"blackNormal14\" align=\"left\">";
    s+=" <table border=\"0\" cellpadding=\"0\" cellspacing=\"1\" width=\"99%\" bgcolor=\"#999999\">";
    s+=" <tr><td height=\"10\" width=\""+result+"%\" bgcolor=\""+cuColor+"\"></td><td width=\""+r2+"%\" bgcolor=\"#CCCCCC\"></td></tr>";
    s+=" </table></td><td   height=\"25\" class=\"blackNormal12\">";
    s+= vc+"("+result+"%)"+"</td></tr>";
    return s;
}


function submitVote_<%=voteId1 %>(){
    var v="";
    $("input[name='voteInput_<%=voteId%>']:checked").each(function(){ 
     v+=$(this).val()+",";}); 
     $.ajax({
			url: '/sapfree/servlet/MessageBoradServlet?method=submitVote&voteId=<%=voteId %>', 
			dataType: 'text',
			type: "post",
			data:{vote:v},
			error:function(XMLHttpRequest,textStatus,errorThrown){alert(errorThrown);},
			success: function(data) {
			     getJsonVote_<%=voteId1 %>('<%=voteId %>');
			}
		});

} 

function addOption_<%=voteId1 %>(){
   var name=prompt("请输入选项名","");
   if(name==null||name==""){
   return
   }
    $.ajax({
			url: '/sapfree/servlet/MessageBoradServlet?method=addOption&voteId=<%=voteId %>', 
			dataType: 'text',
			type: "post",
			data:{voteOption:name},
			error:function(XMLHttpRequest,textStatus,errorThrown){alert(errorThrown);},
			success: function(data) {
			     getJsonVote_<%=voteId1 %>('<%=voteId %>');
			}
	});
}
</script>

 <tr id="voteWait_<%=voteId1 %>"> 
	 <td height="30" colspan="3" style="font-size:12px" align=center><img src="../include/images/loading.gif" /> 加载中……</td>
  </tr>
          
 
   <tr id="option_<%=voteId1 %>" > 
	  <td colspan="3"></td>
  </tr>       
         
                
                <%
				if("false".equals(isclosed)){
				%>
                <tr>
                	<td height="35" colspan="2" class="blackNormal12">
                    <table border="0" cellpadding="0" cellspacing="0" width="100%">
                    	<tr>
                        	<td width="49%" align="right" height="35">
                            <table border="0" cellpadding="0" cellspacing="0" width="60">
                                <tr>
                                    <td width="60" height="20" bgcolor="f6891f" align="center" style="cursor:hand;">
                                        <span style="color:#FFFFFF; font-weight:bold; font-size:12px;" onclick="submitVote_<%=voteId1 %>()">投票</span>                            		</td>
                                </tr>
                            </table>                            </td>
                            <td width="2%"></td>
                            <td width="49%" align="left">
                            <table border="0" cellpadding="0" cellspacing="0" width="80">
                                <tr>
                                    <td width="80" height="20" bgcolor="f6891f" align="center" style="cursor:hand;">
                                        <span style="color:#FFFFFF; font-weight:bold; font-size:12px;" onclick="addOption_<%=voteId1 %>()">添加选项</span>  
									</td>
                                </tr>
                            </table>                            
                            </td>
	                  </tr>
 
	                 </table>
                 
                </td>
                </tr>
                <%
				}
				%>
                
                
                <script>
                    getJsonVote_<%=voteId1 %>('<%=voteId %>');
                </script>